package com.beisita.web.controller;

import com.beisita.annotation.WebLog;
import com.beisita.web.controller.common.BaseActionAbstract;
import com.beisita.web.vo.UserVO;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import java.util.HashMap;
import java.util.Map;

import static org.springframework.http.MediaType.APPLICATION_JSON_UTF8_VALUE;

@Controller
@EnableWebMvc
public class UserController extends BaseActionAbstract {
    @Value("${name}")
    private String name;

    private String cname;

    @GetMapping("/getOne")
    @ResponseBody
    @WebLog(name = "查询", intoDb = false)
    public String getOne(Long id, String name) {
        return "1234";
    }

    @RequestMapping({"/index"})
    public String index(Model model) {
        model.addAttribute("name", "Ryan");
        return "index";
    }

    @GetMapping({"/home"})
    public String html() {
        return "redirect:home.html";
    }

    @RequestMapping({"/"})
    public String hello(Model model) {
        model.addAttribute("name", this.name);
        return "hello";
    }

    @RequestMapping({"/json"})
    @ResponseBody
    public Map<String, Object> json() {
        Map<String, Object> map = new HashMap<>();
        map.put("name", "Ryan");
        map.put("age", "18");
        map.put("sex", "man");
        map.put("customeName", this.name);
        map.put("cName", this.cname);
        return map;
    }

    @PostMapping(value = "/user/create", produces = APPLICATION_JSON_UTF8_VALUE, consumes = APPLICATION_JSON_UTF8_VALUE)
    public void creteUser(@RequestBody @Validated UserVO userVO) {

    }

    @PostMapping(value = "/user/update", produces = APPLICATION_JSON_UTF8_VALUE, consumes = APPLICATION_JSON_UTF8_VALUE)
    public void updateUser(@RequestBody @Validated UserVO userVO) {
        UserVO user = null;
        //user = userDao.selectById(userId);
        Assert.notNull(user, "用户不存在！");
    }
}
